<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
//存数据&取数据-标准写法
/*    
localStorage.setItem('username', 'zs')
console.log(localStorage.getItem('username'))
*/
//存数据&取数据-简化写法
/*
localStorage.age = 20 
console.log(localStorage.age)
*/ 
//localStorage不能直接存对象/数组,需要把对象和数组转换成字符串
// localStorage.channels =  JSON.stringify([{id:1 ,name:"html"}, {id:2,name:"js"}]) 
// 如果本地存储没有channels,取值会是什么JSON.parse("{username: 'zs'}")

//处理localStorage当中没有数据-方式1用try-catch
/*
let channels =  []
try{
    channels =  JSON.parse(localStorage.channels) 
}catch(e){
    console.log(e)
}
*/
//处理localStorage当中没有数据-方式2用三元表达式
// let channels =  JSON.parse(localStorage.channels?localStorage.channels:'[]') 
//处理localStorage当中没有数据-方式3用或者运算符
let channels =  JSON.parse(localStorage.channels || '[]') 
console.log(channels)

</script>
</html>